<?php

namespace App\Http\Controllers\Admin;

use App\Services\WxCardQrcodeService;
use Illuminate\Http\Request;

/**
 * 微信卡券-投放二维码
 * @package App\Http\Controllers\Admin
 */
class WxCardQrcodeController extends BaseController
{
    protected $wxCardQrcodeSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->wxCardQrcodeSer = new WxCardQrcodeService();
    }

    /**
     * 投放二维码列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxCardQrcode::select('*');
            $keyword = $request->get('keyword', '');
            if ($keyword) {
                $query->where('title', 'like', '%' . $keyword . '%');
            }
            $data = $query->orderBy('id', 'DESC')->paginate($pageSize)->toArray();
            if ($data['data']) {
                $qrcodeList = $data['data'];
                foreach ($qrcodeList as $k => $v) {
                    $v['type_str'] = $this->wxCardQrcodeSer->getTypeStr($v['type']);
                    $v['status_str'] = $this->wxCardQrcodeSer->getStatusStr($v['status_is']);
                    $qrcodeList[$k] = $v;
                }
                $data['data'] = $qrcodeList;
            }

            return $data;
        }
        //微信卡券列表
        $this->viewData['typeList'] = $this->wxCardQrcodeSer->getTypeList();
        return view('admin.wxcardqrcode.index', $this->viewData);
    }

    /**
     * 保存投放二维码数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function save(\App\Http\Requests\AdminWxCardQrcodePost $request)
    {
        try {
            $qrcodeId = $request->input('id', 0);
            $rs = $this->wxCardQrcodeSer->save($request);
            if ($rs) {
                return $this->showMsg('success', ($qrcodeId ? '编辑' : '添加') .'投放二维码成功', [], 'admin.wxcardqrcode.index');
            }
            throw new \Exception(($qrcodeId ? '编辑' : '添加') . '投放二维码失败');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 删除投放二维码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function destroy(Request $request){
        try {
            $qrcodeId = $request->input('id', 0);
            $rs = $this->wxCardQrcodeSer->destory($qrcodeId);
            if ($rs) {
                return $this->showMsg('success', '删除投放二维码成功', [], 'admin.wxcardqrcode.index');
            }
            throw new \Exception('删除投放二维码失败');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 生成投放二维码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function publish(Request $request)
    {
        try {
            $qrcodeId = $request->input('id', 0);
            $rs = $this->wxCardQrcodeSer->publish($qrcodeId);
            if ($rs) {
                return $this->showMsg('success', '生成投放二维码成功', [], 'admin.wxcardqrcode.index');
            }
            throw new \Exception('生成投放二维码失败');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
}
